﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AppCode_Admin_UpdateEmployee : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["userNameAdmin"] == null)
            {
                Response.Redirect("..\\Home\\HomePage.aspx");
            }
        }
        if (!IsPostBack)
        {
            String id = Request.QueryString["id"];
            String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
            SqlConnection sqlConnect = new SqlConnection(connect);
            sqlConnect.Open();
            SqlCommand sqlCommand = new SqlCommand("Select *from EmpRegister where Empno=" + id, sqlConnect);
            SqlDataReader reader = sqlCommand.ExecuteReader();
            reader.Read();
            txtDesignation.Text = reader.GetString(1);
            txtJoinDate.Text = reader.GetDateTime(2).ToString();
            txtSalary.Text = reader.GetDecimal(3).ToString();
            txtFirstName.Text = reader.GetString(4);
            txtLastName.Text = reader.GetString(5);
            txtUserName.Text = reader.GetString(6);
            txtAddress.Text = reader.GetString(8);
            txtContact.Text = reader.GetString(9);
            txtState.Text = reader.GetString(10);
            txtCountry.Text = reader.GetString(11);
            txtCity.Text = reader.GetString(12);
            sqlConnect.Close();
        }
        this.SetVisible();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Boolean check = false;

        if (txtDesignation.Text.Equals(""))
        {
            Label2.Visible = true;
            check = true;
        }
        if (txtJoinDate.Text.Equals(""))
        {
            Label4.Visible = true;
            check = true;
        }
        if (txtSalary.Text.Equals(""))
        {
            Label6.Visible = true;
            check = true;
        }
        if (txtFirstName.Text.Equals(""))
        {
            Label8.Visible = true;
            check = true;
        }
        if (txtLastName.Text.Equals(""))
        {
            Label10.Visible = true;
            check = true;
        }
        if (txtUserName.Text.Equals(""))
        {
            Label12.Visible = true;
            check = true;
        }
        if (txtAddress.Text.Equals(""))
        {
            Label16.Visible = true;
            check = true;
        }
        if (txtContact.Text.Equals(""))
        {
            Label18.Visible = true;
            check = true;
        }
        if (txtState.Text.Equals(""))
        {
            Label20.Visible = true;
            check = true;
        }
        if (txtCountry.Text.Equals(""))
        {
            Label22.Visible = true;
            check = true;
        }
        if (txtCity.Text.Equals(""))
        {
            Label24.Visible = true;
            check = true;
        }
        if (!check)
        {
            String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
            SqlConnection sqlConnect = new SqlConnection(connect);
            sqlConnect.Open();
            SqlCommand sqlCommand = new SqlCommand("Update EmpRegister Set Designation=@Designation,Joindate=@Joindate,Salary=@Salary,FirstName=@FirstName,LastName=@LastName,[Address]=@Address,ContactNo=@ContactNo,[State]=@State,Country=@Country,City=@City where EmpNo=" + Request.QueryString["id"], sqlConnect);
            sqlCommand.Parameters.AddWithValue("@Designation", txtDesignation.Text);
            sqlCommand.Parameters.AddWithValue("@Joindate", txtJoinDate.Text);
            sqlCommand.Parameters.AddWithValue("@Salary", Convert.ToDecimal(txtSalary.Text));
            sqlCommand.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
            sqlCommand.Parameters.AddWithValue("@LastName", txtLastName.Text);
            sqlCommand.Parameters.AddWithValue("@Address", txtAddress.Text);
            sqlCommand.Parameters.AddWithValue("@ContactNo", txtContact.Text);
            sqlCommand.Parameters.AddWithValue("@State", txtState.Text);
            sqlCommand.Parameters.AddWithValue("@Country", txtCountry.Text);
            sqlCommand.Parameters.AddWithValue("@City", txtCity.Text);
            sqlCommand.ExecuteNonQuery();
            sqlConnect.Close();
            Response.Redirect("EmployeeDetails.aspx");
        }
    }

    public void SetVisible()
    {
        Label2.Visible = false;
        Label4.Visible = false;
        Label6.Visible = false;
        Label8.Visible = false;
        Label10.Visible = false;
        Label12.Visible = false;
        Label16.Visible = false;
        Label18.Visible = false;
        Label20.Visible = false;
        Label22.Visible = false;
        Label24.Visible = false;
        Label25.Visible = false;
    }
}